<?php
use com\gdl\common\dataobject\common\ResultDO; 
use com\gdl\newagk\interfaces\QbankPracticeServiceIf;
use com\gdl\newagk\dataobject\qbankpractice\QbankDTO; 
use com\gdl\newagk\dataobject\qbankpractice\QbankQuestionDTO;
require(dirname(dirname(__FILE__)) ."/php_sdk-master/include.php");
use MagicCrypt\MagicCrypt;
/**
 * 题库练习App接口 
 * 包含标签列表 错题 收藏 赛选等
 */
class api_QbankPracticeService extends api_Abstract implements QbankPracticeServiceIf{ 
    /**
     * 添加做题记录
     */
    public function setQbankQuestion(QbankDTO $qbank)
    { 
        $platformId = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : 0;
        $data = $qbank->data ? $qbank->data : ""; # 包含插入的信息
		$result = new ResultDO();
        $result->success = true;
		$result->message = "您已添加，请勿重复添加";
        $result->code = 400; 
        $result->data = []; 
		
        try{
			$praciticeModel = new model_newagk_pracitice();
			$errorModel = new model_newagk_errorpracitice();
			 if(empty($data['judge'])){
			 	$data['judge'] = 0;
			 }
			// var_dump($data);exit;
			
			$inserData = $data;
			if($data['type']!='error'){
				# 查询如果做题记录表已经有该试题了，则修改不删除
				$wheres['typeId'] = $data['typeId'];
				$wheres['qbankId'] = $data['qbankId'];
				$wheres['platformId'] = $data['platformId'];
				$wheres['identityId'] = $data['identityId'];
				$wheres['questionId'] = $data['questionId'];
				$wheres['type'] = $data['type'];
				$deleteRes = $praciticeModel->delete($wheres);
				# 写入记录
				$res = $praciticeModel->insert($data);
				if($data['type']=='order' || $data['type']=='special'){
					if($data['judge']==0){
						# 先删错题
						$delWhere['typeId'] = $data['typeId'];
						$delWhere['qbankId'] = $data['qbankId'];
						$delWhere['platformId'] = $data['platformId'];
						$delWhere['identityId'] = $data['identityId'];
						$delWhere['questionId'] = $data['questionId'];
						$errorModel->delete($delWhere);
						$data['knowId'] = "";
						$data['judge'] = "";
						$errorRet = $errorModel->insert($data);
					}
				}
			}	
			if($data['type']=='error'){
				$update['knowId'] = $data['knowId'];
				$update['judge'] = $data['judge'];
				$wheres['platformId'] = $platformId;
				$wheres['identityId'] = $data['identityId'];
				$wheres['qbankId'] = $data['qbankId'];
				$wheres['questionId'] = $data['questionId'];
				$upErroer = $errorModel->update($wheres,$update);
			}
			if($res||$errorRet||$upErroer){
				$result->code = 200;
				$result->message = "添加做题记录成功";
			}
			
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
	/**
     * 专项练习标签接口
	 * 专项练习第一个标签接口
     */
    public function specialTagList(QbankDTO $qbank)
    { 
        $qbankId = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : "";
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0;
		$num = $qbank->num ? $qbank->num : 10;
		$result = new ResultDO();
        $result->success = true;
		$result->message = "获取失败";
        $result->code = 400; 
        $result->data = [];
        try{
			$qbankModel = new model_newagk_qbank();
			$questionModel = new model_newagk_questions();
			$quesTypeModel = new model_newagk_questype();
			$sectionModel = new model_newagk_sections();
			# 章节
			$where = "isdelete=0 and status=1 and qbankId=".$qbankId;
			$sectionList = $sectionModel->select($where,'id,seName')->items;
			$newSectionList =[];
			for($i=0; $i<count($sectionList); $i++){
				$qWhere = "isdelete=0 and status=1 and qbankId=".$qbankId." and sectionId=".$sectionList[$i]['id'];
				$num = $questionModel->select($qWhere,'count(id) as num')->items;
				$sectionList[$i]['count'] = $num[0]['num']?$num[0]['num']:0;
				if($sectionList[$i]['count']>0){
					$newSectionList[] = $sectionList[$i];
				}
			}
			# 选项
			$typeWhere = "isdelete=0 and status=1";
			$typeList = $quesTypeModel->select($typeWhere, 'id,type')->items;
			$newTypeList = [];
			for($j=0; $j<count($typeList); $j++){
				$bWhere = "isdelete=0 and status=1 and qbankId=".$qbankId." and typeId=".$typeList[$j]['id'];
				$num = $questionModel->select($bWhere,'count(id) as num')->items;
				$typeList[$j]['num'] = $num[0]['num']?$num[0]['num']:0;
				if($typeList[$j]['num']>0){
					$newTypeList[] = $typeList[$j];
				}
			}
			$resultList['msg'] = "获取成功";
			$resultList['data'] = json_encode(['typeList'=>$newTypeList, 'sectionList'=>$newSectionList]);
			$result->code = 200;
			if($resultList){
				
				$result->message = "获取成功";
				$result->data[]= $resultList;
			}else{
				$result->message = "没有更多数据了";
			}
			
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
	/**
	 * 题库试题标签赛选接口
     */
    public function qbankTagList(QbankDTO $qbank)
    { 
        $qbankid = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : "";
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0;
		$result = new ResultDO();
        $result->success = true;
		$result->message = "请选择题库";
        $result->code = 400; 
        $result->data = []; 
		if(empty($qbankid) || empty($identity_id)){
			$result->message = "请选择题库";
			return $result;
		}
        try{
			# 试题类型
			$typeModel = new model_newagk_quesType();
			$typeList = $typeModel->select('isdelete=0 and status=1','id,type')->items;
			# 章节
			$sectionModel = new model_newagk_sections();
			$sectionList = $sectionModel->select('isdelete=0 and status=1','id,seName')->items;
			#题目标签
			$qbankTagModel = new model_newagk_qbanktag();
			$condition = "isdelete=0 and  type=1";
			$filed = "id,pid,tagName";
			$tagList = $qbankTagModel->select($condition,$filed)->items;
			$newList = $this->getTree($tagList,0);
			$resultList['msg'] = "获取成功";
			$resultList['data'] = json_encode(['typeList'=>$typeList, 'sectionList'=>$sectionList,'tagList'=>$newList]);
			$result->code = 200;
			if($resultList){
				$result->message = "获取成功";
				$result->data[]= $resultList;
			}else{
				$result->message = "没有更多数据了";
			}
			 
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
	/**
	 * 错题题库列表接口
     */
    public function errorQbankList(QbankDTO $qbank)
    { 
        $platformId = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : "";
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0; 
		$adminReg = $qbank->filed ? $qbank->filed : "";
		$offset =  $qbank->offset ? gdl_lib_BaseUtils::getStr($qbank->offset, 'int') : 0; 
		$num =  $qbank->num ? gdl_lib_BaseUtils::getStr($qbank->num, 'int') : 10; 
		$result = new ResultDO();
        $result->success = true;
		$result->message = "获取列表失败";
        $result->code = 400; 
        $result->data = []; 
		# 分页
		if($offset=='0') $offset=1;
		$rows = $num;
		$limit = ($offset - 1) * $rows . "," . $rows;
		$group = ' group by agk_qbank.id order by sort desc limit '.$limit;
		if(empty($platformId) || empty($identity_id)){
			$result->message = "您还未登录，请先登录";
			return $result;
		}
		
        try{
			# 查询题库可能有问题
			$qbankModel = new model_newagk_qbank();
			$praciticeModel = new model_newagk_errorpracitice();
			$leftJoin = array('agk_error_pracitice as pr'=>'agk_qbank.id=pr.qbankId');
			$condition = "agk_qbank.isdelete = 0 and (agk_qbank.admin_reg in ('{$adminReg}') or agk_qbank.platformId=".$platformId." )";
			$qbankList = $qbankModel->select($condition, 'agk_qbank.id,agk_qbank.name',$group,'',$leftJoin)->items;
			$newQbankList = [];
			for($i=0; $i<count($qbankList); $i++){
				$where = "isdelete=0 and identityId=".$identity_id." and qbankId=".$qbankList[$i]['id'];
				$num = $praciticeModel->select($where,'count(id) as num')->items; 
				$qbankList[$i]['count'] = $num[0]['num']?$num[0]['num']:0;
				if($qbankList[$i]['count']>0){
					$newQbankList[] = $qbankList[$i];
				}
			}
			$result->code = 200;
			if($qbankList){
				$result->message = "获取成功";
				$result->data = $newQbankList;
			}else{
				$result->message = "没有更多数据了";
			}
        } catch (Exception $e) {
			$result->success = false;
			$result->code = $e->getCode();
			$result->message = $e->getMessage(); 
        }
        return $result; 
    }	
	
	/**
	 * 我的收藏题库列表
     */
    public function collectionQbankList(QbankDTO $qbank)
    { 
        $platformId = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : "";
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0; 
		$adminReg = $qbank->filed ? $qbank->filed : "";
		$offset =  $qbank->offset ? gdl_lib_BaseUtils::getStr($qbank->offset, 'int') : 0; 
		$num =  $qbank->num ? gdl_lib_BaseUtils::getStr($qbank->num, 'int') : 10; 
		$result = new ResultDO();
        $result->success = true;
		$result->message = "获取列表失败";
        $result->code = 400; 
        $result->data = []; 
		# 分页
		if($offset=='0') $offset=1;
		$rows = $num;
		$limit = ($offset - 1) * $rows . "," . $rows;
		$group = 'group by agk_qbank.id order by sort desc limit '.$limit;
		if(empty($platformId) || empty($identity_id)){
			$result->message = "您还未登录，请先登录";
			return $result;
		}
		
        try{
			# 查询题库可能有问题
			$qbankModel = new model_newagk_qbank();
			$collectionModel = new model_newagk_collection();
			$leftJoin = array('agk_pracitice as pr'=>'agk_qbank.id=pr.qbankId');
			$condition = "agk_qbank.isdelete = 0 and (agk_qbank.admin_reg in ('{$adminReg}') or agk_qbank.platformId=".$platformId." )";
			$qbankList = $qbankModel->select($condition, 'agk_qbank.id,agk_qbank.name',$group,'',$leftJoin)->items; 
			$newQbankList = [];
			for($i=0; $i<count($qbankList); $i++){
				$where = "isdelete=0 and identityId=".$identity_id." and qbankId=".$qbankList[$i]['id']." and questionId<>0";
				$num = $collectionModel->select($where,'count(id) as num')->items;
				$qbankList[$i]['count'] = $num[0]['num']?$num[0]['num']:0;
				if($qbankList[$i]['count']>0){
					$newQbankList[] = $qbankList[$i];
				}
			}
			if($qbankList){
				$result->code = 200;
				$result->message = "获取成功";
				$result->data = $newQbankList;
			}
        } catch (Exception $e) {
			$result->success = false;
			$result->code = $e->getCode();
			$result->message = $e->getMessage(); 
        }
        return $result; 
    }
	
	/**
	 * 做了多少题
     */
    public function getQbankNum(QbankDTO $qbank)
    { 
        $qbankId = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : "";
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0;
		$result = new ResultDO();
        $result->success = true;
		$result->message = "没有更多数据了";
        $result->code = 400; 
        $result->data = []; 
		if(empty($qbankId) || empty($identity_id)){
			$result->message = "请先登录";
			return $result;
		}
		
        try{
			# 查询该题库有多少题
			$qbankModel = new model_newagk_qbank();
			$qbankCount = $qbankModel->select('id='.$qbankId,'total')->items;
			
			$praciticeModel = new model_newagk_pracitice();
			#作对多少
			$successCount = $praciticeModel->select('isdelete=0 and qbankId='.$qbankId.' and judge=1 and (type="special" or type="order") and identityId='.$identity_id,'count(id) as num')->items;
			#做错多少
			$errorCount = $praciticeModel->select('isdelete=0 and qbankId='.$qbankId.' and judge=0 and (type="special" or type="order") and identityId='.$identity_id,'count(id) as num')->items;
			#未知多少
			$notCount = $praciticeModel->select('isdelete=0 and qbankId='.$qbankId.' and judge=2 and (type="special" or type="order") and identityId='.$identity_id,'count(id) as num')->items;
			$numCount = $notCount[0]['num']+$errorCount[0]['num']+$successCount[0]['num'];
			if($numCount>$qbankCount[0]['total']){
				$numCount = $qbankCount[0]['total']?$qbankCount[0]['total']:0;
			} 
			$resultList['msg'] = '获取成功';
			$resultList['data'] = json_encode(['numCount'=>$numCount,'success'=>$successCount[0]['num']?$successCount[0]['num']:0, 'errorCount'=>$errorCount[0]['num']?$errorCount[0]['num']:0, 'notCount'=>$notCount[0]['num']?$notCount[0]['num']:0]);
			
			if($resultList){
				$result->message = "获取成功";
				$result->code = 200;
				$result->data[] = $resultList;
			}
			 
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
	
	/**
	 * 添加取消收藏
     */
    public function setCollection(QbankDTO $qbank)
    { 
        $platformId = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : ""; 
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0;
		$qbankId = $qbank->filed ? $qbank->filed : "";
		$questionId =  $qbank->update ? gdl_lib_BaseUtils::getStr($qbank->update) : 0;
		$type = $qbank->where ? $qbank->where : "";
		
		$result = new ResultDO();
        $result->success = true;
		$result->message = "请勿重复操作";
        $result->code = 400; 
        $result->data = []; 
		if(!$platformId){
			$result->message = "您还未选择平台";
			return $result;
		}
		
        try{
			$collectionModel = new model_newagk_collection();
			# 设置
			if($type=='set'){
				$insertData = [
					'qbankId'=>$qbankId,
					'questionId'=>$questionId,
					'identityId'=>$identity_id,
					'platformId'=>$platformId,
					'createTime'=>time(),
					'isdelete'=>0
				]; 
				$set = $collectionModel->insert($insertData);
			
			#删除
			}
			if($type=='del'){
				$upData = ['isdelete'=>'1'];
				$where = "qbankId=".$qbankId." and questionId=".$questionId." and identityId=".$identity_id." and platformId =".$platformId." and isdelete=0";
				$del= $collectionModel->update($where, $upData); 
			}
			$result->code = 200;
			if($del){
				$result->message = "已移除收藏";
			}
			if($set){
				$result->message = "收藏成功";
			}
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
	
	
	/**
	 * 移除错题
     */
    public function deleteError(QbankDTO $qbank)
    { 
        $platformId = $qbank->id ? gdl_lib_BaseUtils::getStr($qbank->id) : ""; 
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0; 
		$questionId =  $qbank->update ? gdl_lib_BaseUtils::getStr($qbank->update) : 0; 
		$result = new ResultDO();
        $result->success = true;
		$result->message = "您已移除，请勿重复操作";
        $result->code = 400; 
        $result->data = []; 
		if(empty($platformId)){
			$result->message = "您还没有选择平台";
			return $result;
		}
		
        try{
			 $praciticeModel = new model_newagk_errorpracitice();
			 $where = "identityId=".$identity_id." and questionId=".$questionId;
			 //$upData = ['isdelete'=>1];
			 //$res = $praciticeModel->update($where, $upData);
			 $res = $praciticeModel->delete($where);
			 if($res){
				 $result->code = 200;
				 $result->message = "已移除错题";
			 }
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
	/**
	* 公共抽提接口题库列表
	* 参数对应可能有点混乱请仔细核对
	* order：顺序练习获取该题库下所有试题
	* special：专项练习 根据选择试题种类 下属章节获取试题
	* error :错题练习 查询当前题库下我的所有试题
	* collection: 收藏，查询我当前收藏下的所有试题
	*/
	public function getQuestionList(QbankQuestionDTO $qbank){
		# 常规
		$platformId = $qbank->praformid ? gdl_lib_BaseUtils::getStr($qbank->praformid) : ""; 
        $identity_id =  $qbank->identity_id ? gdl_lib_BaseUtils::getStr($qbank->identity_id, 'int') : 0; 
		$att = $qbank->att ? gdl_lib_BaseUtils::getStr($qbank->att) : ""; 
		$qbankId = $qbank->qbankid ? gdl_lib_BaseUtils::getStr($qbank->qbankid) : ""; 
		$sectionId = $qbank->sectionid ? gdl_lib_BaseUtils::getStr($qbank->sectionid) : ""; 
		$typeId = $qbank->typeid ? gdl_lib_BaseUtils::getStr($qbank->typeid) : ""; 
		
		# 赛选值
		$se_typeId = $qbank->str1 ? gdl_lib_BaseUtils::getStr($qbank->str1) : ""; 
		$se_sectionId = $qbank->str2 ? gdl_lib_BaseUtils::getStr($qbank->str2) : ""; 
		$se_tag = $qbank->str3 ? gdl_lib_BaseUtils::getStr($qbank->str3) : "";  # 父亲标签 逗号分隔
		$se_tagVal = $qbank->str4 ? gdl_lib_BaseUtils::getStr($qbank->str4) : "";  # 子标签 逗号分隔
		
		
		$result = new ResultDO();
        $result->success = true;
		$result->message = "获取试题异常，请重新获取";
        $result->code = 400; 
        $result->data = []; 
		try{
			$questionModel = new model_newagk_questions();
			$collectionModel = new model_newagk_collection();
			$praciticeModel = new model_newagk_pracitice();
			# 获取赛选值
			$condition = "isdelete=0 and status=1 and qbankId=".$qbankId;
			if(!empty($se_typeId) && $se_typeId!="0"){
				$condition.= " and typeId=".$se_typeId;
			}
			if(!empty($se_sectionId) && $se_sectionId!="0"){
				$condition.=" and sectionId=".$se_sectionId;
			}
			#专项练习 试题类型
			if(!empty($typeId) && $typeId!="0"){
					$condition.= " and typeId=".$typeId;
			}
			#专项练习 章节选择
			if(!empty($sectionId) && $sectionId!="0"){
					$condition.= " and sectionId=".$sectionId;
			}
			
			# 如果选择全部单传tag ，如果选择个别则不传只传子分类id
			if(!empty($se_tag)){
				$tagArray = explode(",",$se_tag);
				$sqlStr = " ";
				
				for($i=0; $i<count($tagArray); $i++){
					if($i==count($tagArray)-1){
						$sqlStr.= " quesTag like '%".$tagArray[$i]."%'";
					}else{
						$sqlStr.= " quesTag like '%".$tagArray[$i]."%' or ";
					} 
				}
				if(!empty($se_tagVal)){
					$condition.= " and (".$sqlStr." or "; 
				}else{
					$condition.= " and (".$sqlStr.") "; 
				}
				
			} 
			# 赛选子分类
			if(!empty($se_tagVal)){
				$tagValArray = explode(",",$se_tagVal);
				$sqlValStr = "";
				for($i=0; $i<count($tagValArray); $i++){
					if($i==count($tagValArray)-1){
						$sqlValStr.= " quesTagVal like '%".$tagValArray[$i]."%'";
					}else{
						$sqlValStr.= " quesTagVal like '%".$tagValArray[$i]."%' or ";
					} 
				}
				if(!empty($se_tag)){
					$condition.= " ".$sqlValStr.") "; 
				}else{
					$condition.= " and (".$sqlValStr.") "; 
				}
			}
			# 把每次的试题放进练习试卷记录表里
			$testPaperModel = new model_newagk_testpaper();
			
			# 当前题库下所有试题id
			$allQuestionList = $questionModel->select($condition,'id')->items;
			$allIdArray = []; # 该题库下所有的试题id
			$allIdStr = ""; # 拼接的字符串 通过该id查询试题
			if($allQuestionList){
				for($i=0; $i<count($allQuestionList);$i++){
						$allIdArray[] = $allQuestionList[$i]['id'];
				}
				$allIdStr = implode(",",$allIdArray);
			}else{
				$result->message = "无法生成试题，该类型题目不存在"; 
				return $result;
			} 
			$drawquestion = new service_drawquestion();
			
			
			# 顺序练习（2） 接口和赛选接口
			if($att=='order'){ 
				# 插入练习试卷表里
				$markBookOne = $testPaperModel->selectOne('isdelete=0 and qbankId='.$qbankId.' and identityId='.$identity_id.' and type="'.$att.'"','bookMark');
				$testPaper = ['qbankId'=>$qbankId,'quesId'=>serialize($allIdArray),'identityId'=>$identity_id,'isdelete'=>0,'type'=>$att,'bookMark'=>$markBookOne['bookMark']];
				$testPaperModel->update('isdelete=0 and qbankId='.$qbankId.' and identityId='.$identity_id.' and type="'.$att.'"',['isdelete'=>1]);
				$testPaperModel->insert($testPaper);
			
				$Questions = $drawquestion->getQuestionsInfo($allIdArray,$qbankId,$identity_id,1,true,$att);
				$count =0; 
				foreach($Questions as $i){
					$count+=count($i);
				} 
				
				$datac= ['list'=>$Questions?$Questions:['single'=>array()],'count'=>$count];
				
				$result->message = json_encode($datac); 
				$result->code = 200;
				return $result;
			}
			# 专项练习 试题类型 章节选择
			if($att=='special'){
				# 插入练习试卷表里
				# 
				$markBookOne = $testPaperModel->selectOne('isdelete=0 and qbankId='.$qbankId.' and identityId='.$identity_id.' and type="'.$att.'"','bookMark');
				$testPaper = ['qbankId'=>$qbankId,'quesId'=>serialize($allIdArray),'identityId'=>$identity_id,'isdelete'=>0,'type'=>$att,'bookMark'=>$markBookOne['bookMark']];
				$testPaperModel->update('isdelete=0 and qbankId='.$qbankId.' and identityId='.$identity_id.' and type="'.$att.'"',['isdelete'=>1]);
				$testPaperModel->insert($testPaper);
				
				$Questions = $drawquestion->getQuestionsInfo($allIdArray,$qbankId,$identity_id,1,true,$att);
				$count =0; 
				foreach($Questions as $i){
					$count+=count($i);
				} 
				$datac= ['list'=>$Questions?$Questions:['single'=>array()],'count'=>$count];
				
				$result->message = json_encode($datac); 
				$result->code = 200;
				return $result;
			} 
			# 错题
			if($att=='error'){ 
				$errorModel = new model_newagk_errorpracitice();
				$errorWhere = "isdelete=0 and qbankId=".$qbankId." and identityId=".$identity_id." and questionId in (".$allIdStr.")";
				$errorId = $errorModel->select($errorWhere, 'id,questionId')->items;
				
				//var_dump($errorId);exit;
				$inStrArray = [];
				for($i=0; $i<count($errorId);$i++){
						$inStrArray[] = $errorId[$i]['questionId'];
				}
				$Questions = $drawquestion->getQuestionsInfo($inStrArray,$qbankId,$identity_id,1,true,$att);
				$count =0; 
				foreach($Questions as $i){
					$count+=count($i);
				}

				# 插入练习试卷表里
				$testPaper = ['qbankId'=>$qbankId,'quesId'=>serialize($inStrArray),'identityId'=>$identity_id,'isdelete'=>0,'type'=>$att];
				$testPaperModel->update('isdelete=0 and qbankId='.$qbankId.' and identityId='.$identity_id.' and type="'.$att.'"',['isdelete'=>1]);
				$testPaperModel->insert($testPaper);
				//$types= ['1'=>'single','2'=>'more','3'=>'judge','4'=>'gapFilling','5'=>'subjectivity'];
				$datac= ['list'=>$Questions?$Questions:['single'=>array()],'count'=>$count];
				$result->message = json_encode($datac); 
				$result->code = 200;
				return $result;
			}
			
			# 我的收藏
			if($att=='collection'){
				$collectionWhere = "isdelete=0 and qbankId=".$qbankId." and identityId=".$identity_id." and questionId in (".$allIdStr.")";
				$collectionId = $collectionModel->select($collectionWhere,'id,questionId')->items;
				
				$inStrArray = [];
				for($i=0; $i<count($collectionId);$i++){
						$inStrArray[] = $collectionId[$i]['questionId'];
				}
				$Questions = $drawquestion->getQuestionsInfo($inStrArray,$qbankId,$identity_id,1,true,$att);
				$count =0; 
				foreach($Questions as $i){
					$count+=count($i);
				}
				# 插入练习试卷表里
				$testPaper = ['qbankId'=>$qbankId,'quesId'=>serialize($inStrArray),'identityId'=>$identity_id,'isdelete'=>0,'type'=>$att];
				$testPaperModel->update('isdelete=0 and qbankId='.$qbankId.' and identityId='.$identity_id.' and type="'.$att.'"',['isdelete'=>1]);
				$testPaperModel->insert($testPaper);
				
				$datac= ['list'=>$Questions?$Questions:['single'=>array()],'count'=>$count];
				$result->message = json_encode($datac); 
				$result->code = 200;
				return $result;
			}
		
		}catch(Exception $e){
			$result->success = false;
			$result->code = $e->getCode();
			$result->message = $e->getMessage();
		}
		return $result;	
	}
	/**
	* 练习赛选抽题接口
	*/
	public function raceQuestionList(QbankQuestionDTO $qbank){
		
	}
	/**
	*  找儿子
	*/
	public static function getTree($data, $pId){
		$tree = '';
		foreach($data as $k => $v)
		{
		  if($v['pid'] == $pId)
		  {        //父亲找到儿子
		   $v['pid'] = self::getTree($data, $v['id']);
		   $tree[] = $v;
		   //unset($data[$k]);
		  }
		}
		return $tree;
	}
   
}
